#
# (c) Copyright 2020 CORSIKA Project, corsika-project@lists.kit.edu
#
# This software is distributed under the terms of the 3-clause BSD license.
# See file LICENSE for a full version of the license.
#

"""
Readthedocs script to build documentation remotely.
"""

import sys
import subprocess, os

def configureDoxyfile(template_file, output_file, input_dir, output_dir):
    with open(template_file, 'r') as file :
        filedata = file.read()

    filedata = filedata.replace('@PROJECT_SOURCE_DIR@', input_dir)
    filedata = filedata.replace('@CMAKE_CURRENT_BINARY_DIR@', output_dir)
    filedata = filedata.replace('@CMAKE_BINARY_DIR@', output_dir)

    with open(output_file, 'w') as file:
        file.write(filedata)

def configureDoxyLayout(template_file, output_file, page_url, page_tile):
    with open(template_file, 'r') as file :
        filedata = file.read()

    filedata = filedata.replace('@CORSIKA_WEBPAGE_URL@', page_url)
    filedata = filedata.replace('@CORSIKA_WEBPAGE_TITLE@', page_tile)

    with open(output_file, 'w') as file:
        file.write(filedata)




read_the_docs_build = os.environ.get('READTHEDOCS', None) != None
breathe_projects = {}

if read_the_docs_build:
    build_version = os.environ.get('READTHEDOCS_VERSION', None)
    doc_url = 'https://corsika-8.readthedocs.io/en/'+build_version
    configureDoxyfile("Doxyfile.in", "Doxyfile", "../", "_build/workdir/doxygen")
    configureDoxyLayout("DoxyLayout.in", "DoxyLayout.xml", doc_url , "CORSIKA 8 Webpage")
    subprocess.call('mkdir -p _build/workdir/doxygen; doxygen Doxyfile', shell=True)
    html_extra_path = ['_build/workdir/']
    breathe_projects['CORSIKA8'] = '_build/workdir/doxygen/xml'

    
    

# -- Project information -----------------------------------------------------

project   = u'CORSIKA8'
copyright = u'2021, CORSIKA 8 Collaboration'
author    = u'CORSIKA 8 Collaboration'

# The short X.Y version
version = u'0.0.0'
# The full version, including alpha/beta/rc tags
release = u'prototype-0.1.0'

extensions = [
    'sphinx.ext.todo',
    'breathe',
    'sphinx.ext.mathjax',
    'sphinx.ext.autodoc',
    'sphinx.ext.extlinks',
    'sphinx.ext.autosummary',
    'sphinx.ext.napoleon',
    'recommonmark'
]

breathe_default_project = "CORSIKA8"

# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']

# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This pattern also affects html_static_path and html_extra_path.
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store']

# The suffix(es) of source filenames.
# You can specify multiple suffix as a list of string:
#
# source_suffix = ['.rst', '.md']
source_suffix = '.rst'

# The master toctree document.
master_doc = 'index'

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
#
# This is also used if you do content translation via gettext catalogs.
# Usually you set "language" from the command line for these cases.
language = None

# The name of the Pygments (syntax highlighting) style to use.
pygments_style = 'sphinx'


# -- Options for HTML output -------------------------------------------------

html_theme = 'sphinx_rtd_theme'

html_theme_options = {
    'analytics_id': '',  #  Provided by Google in your dashboard
    'analytics_anonymize_ip': False,
    'logo_only': False,
    'display_version': True,
    'prev_next_buttons_location': 'bottom',
    'style_external_links': False,
    # Toc options
    'collapse_navigation': True,
    'sticky_navigation': True,
    'navigation_depth': 4,
    'includehidden': True,
    'titles_only': False
}

# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ['_static']

htmlhelp_basename = 'C8doc'

# -- Options for LaTeX output ------------------------------------------------

latex_elements = {
    # The paper size ('letterpaper' or 'a4paper').
    #
    # 'papersize': 'letterpaper',

    # The font size ('10pt', '11pt' or '12pt').
    #
    # 'pointsize': '10pt',

    # Additional stuff for the LaTeX preamble.
    #
    # 'preamble': '',

    # Latex figure (float) alignment
    #
    # 'figure_align': 'htbp',
}

# Grouping the document tree into LaTeX files. List of tuples
# (source start file, target name, title,
#  author, documentclass [howto, manual, or own class]).
latex_documents = [
    (master_doc, 'C8-GUIDE.tex', u'CORSIKA 8 Documentation',
     u'CORSIKA 8 Collaboration', 'manual'),
]


# -- Options for manual page output ------------------------------------------

# One entry per manual page. List of tuples
# (source start file, name, description, authors, manual section).
man_pages = [
    (master_doc, 'CORSIKA 8', u'CORSIKA 8 Documentation',
     [author], 1)
]


# -- Options for Texinfo output ----------------------------------------------

# Grouping the document tree into Texinfo files. List of tuples
# (source start file, target name, title, author,
#  dir menu entry, description, category)
texinfo_documents = [
    (master_doc, 'CORSIKA 8', u'CORSIKA 8 Documentation',
     author, 'CORSIKA 8', 'The air shower simulation framework',
     'Miscellaneous'),
]


# -- Extension configuration -------------------------------------------------

napoleon_google_docstring = True
napoleon_include_init_with_doc = True
napoleon_use_keyword = True
napoleon_use_param = True
napoleon_use_rtype = False
napoleon_use_admonition_for_examples = True
# autodoc_docstring_signature=False
